When programs become large, they are hard to understand, debug and edit. Often one wants to break a program into a number of different code files. A project is a tool for accomplishing this. By putting a number of separate code files into a project, they can be compiled and run all together, as one unit.
To create a new project you can either use the menu selection: Project/New
or click on the new project button (the folder that looks like it is
glowing: ) on the speed bar. This brings up a
dialogue box, in which you must do several things:
You need to make sure that the two options, ".rc"
and ".def" are not selected. If they have a check mark next
to them, then they are selected, and you should click once
on the check to remove it. If they look like they do in the image
above, you are fine. Click OK to return to the new project
dialogue box.
You now have created a new project file. You should see your project window somewhere on the screen, with the name of your project at the top (with a ".exe" extension) and just below that with a ".cpp") extension. This signifies that the executable file that is created when you compile you program will have the name [project-name].exe, and that so far there is one code file in your project, named [project-name].cpp. For example, below we have created a project called "myprog", and we see its associated project window.
The project window lists all the code files associated in the
project. When you select Project/Build All or hit the run button
on the speed bar, all the code files in the list are compiled and linked
together into a single program. (And then run.) Since the point of having
a project is to allow multiple code files, we next consider how to add
files to or remove them from the list.
To add a code file to the project, click the right mouse
button on the top line in the project window hierarchy (i.e. the "myprog[.exe]"line).
This gives a menu with an Add option. Selecting Add opens
a dialogue box, which allows you to choose a file to add to the project.
(You can get to this box more quickly by clicking on the top line with the
left mouse button, and hitting the insert key on the
keyboard.)
Select Add node to add a file to the list:
Select or create a file to add:
If you have already written the file, find it on disk just as you
do when you open a file. If you are creating a new file, just give it a
name (ending in ".cpp" preferably). Click OK and the
file should now be in your project. In the example below we have added
another code file, "progA.cpp" to the "myprog"
project:
You need only add your ".cpp" files to the project. Any ".h"
files are included automatically when you compile the program; you do not
need to include them in the project list. In fact, doing so will sometimes
cause errors. (However, if you have declared any variables or defined
any functions in a ".h" file, it must be included in
the project list.)
The project window allows easy access to the files you are working on. Simply double-click on a file's name in the project window, and the file will be opened for you to edit.
You should be aware that closing the project window by does
not close the project, and opening a project does not always give you
a project window. If you have a project open, but your project window is
closed or you can't find it, you can get it back by using the View/Project
menu option.
To run your program, first make sure any one of the project files or the
project window itself is selected. Then hit the speed-bar run button
or select Debug/Run from the menus, just
like when you run a single-file program. Borland will compile your program
and run it (if there are no errors!).
If you get some strange linker errors that you don't think are your fault (often dealing with iostreams) it sometimes helps to use the menu option Project/Build All. This completely recompiles all your code, and makes sure everything is up to date. Then try running the program one more time. If you still get errors, then there probably really is something wrong with your problem. You can get help from a consultant if you're stumped.
Occasionally you may want to remove a file from the project list. This can happen, for instance, if you reorganize your code files, or if you don't want the ".cpp" that the project starts out with. (When you create a project called "progA", for instance, Borland will automatically put a file called "progA.cpp" in the project list. But you may not want such a file, and will need to remove it.)
To remove a file, click the right mouse button on it's
name in the project list, just like when you add a file. Then select Delete
node from the menu that appears. Or, you can click on the file with
the left mouse button, and hit the delete key on the
keyboard.
Unfortunately, you can't print all the files in your project with a single command. You will need to open each file separately and print it out with the File/Print menu option (see Printing your program for details). You can print the output of the project in the same way as you printed the output for a single-file program (again, see Printing your program).